﻿/**************************************************************************
创建时间:	2020/5/14 14:28:19    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using ASmile.Com;
using ASmile.DBModel;
using ASmile.Extend;
using ASmile.Helpers;
using ASmile.Win.Controls;
using ASmile.Win.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ASmile.Win
{
    public static class FormGridViewEx
    {
        private static readonly IGridViewBLL DBConfig;

        static FormGridViewEx()
        {
            DBConfig = ObjectFactory.GetObject<IGridViewBLL>("GridViewBLL");
        }
        /// <summary>
        /// 创建一个GridView的文本框列
        /// </summary>
        /// <param name="text">标题</param>
        /// <param name="pName">数据源名称</param>
        /// <returns></returns>
        private static DataGridViewTextBoxColumn CreateTextBox(Sys_GridColumn colArg) //string text, string pName)
        {
            return new DataGridViewTextBoxColumn
            {
                HeaderText = colArg.ColumnTitle,
                DataPropertyName = colArg.ColumnName
            };
        }
        /// <summary>
        /// 创建一个GridView的CheckBox
        /// </summary>
        private static DataGridViewColumn CreateCheckBox(Sys_GridColumn colArg)
        {
            var col = new DataGridViewCheckBoxColumn
            {
                HeaderText = colArg.ColumnTitle,
                DataPropertyName = colArg.ColumnName,

            };
            if (colArg.DataType.StringEquals("int"))
            {
                col.TrueValue = 1;
                col.FalseValue = 0;
            }

            return col;
        }
        /// <summary>
        /// 创建一个GridView的ComboBox 列
        /// </summary>
        private static DataGridViewColumn CreateComboBox(Sys_GridColumn colArg)
        {
            var col = new DataGridViewComboBoxColumn
            {
                HeaderText = colArg.ColumnTitle,
                DataPropertyName = colArg.ColumnName,
            };
            return col;
        }
        /// <summary>
        /// 初始化GridView列
        /// </summary>
        private static void InitGridColumn(DataGridView dgv, List<Sys_GridColumn> cols)
        {
            if (cols.Exists(s => s.IsReadOnly == false))
            {
                dgv.ReadOnly = false;
            }
            if (cols.Count == 1)
            {
                dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            else if (cols.Exists(s => s.ColumnWidth > 0))
            {
                dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            }

            foreach (var itemCol in cols)
            {
                if (!itemCol.IsVisible) continue;

                if (!itemCol.DgvName.StringEquals(dgv.Name))
                {
                    continue;
                }

                DataGridViewColumn dgvCol;
                EnumHelper.StringToEnum(itemCol.ColumnType, out DgvColumnType colType);

                switch (colType)
                {
                    case DgvColumnType.CheckBox:
                        var col = new DataGridViewCheckBoxColumn();
                        if (itemCol.DataType.StringEquals("int"))
                        {
                            col.TrueValue = 1;
                            col.FalseValue = 0;
                        }
                        dgvCol = col;
                        break;
                    case DgvColumnType.ComboBox:
                        dgvCol = new DataGridViewComboBoxColumn();
                        break;
                    case DgvColumnType.TextBox:
                    default:
                        //dgvCol = CreateTextBox(itemCol);
                        dgvCol = new DataGridViewTextBoxColumn();
                        break;
                }

                if (EnumHelper.StringToEnum(itemCol.Alignment, out DataGridViewContentAlignment alignment))
                {
                    if (alignment != DataGridViewContentAlignment.NotSet)
                        dgvCol.DefaultCellStyle.Alignment = alignment;
                }
                if (EnumHelper.StringToEnum(itemCol.AutoSizeMode, out DataGridViewAutoSizeColumnMode colAutoSizeMode))
                {
                    if (colAutoSizeMode != DataGridViewAutoSizeColumnMode.NotSet)
                    {
                        dgvCol.AutoSizeMode = colAutoSizeMode;
                    }
                }

                if (itemCol.ColumnWidth > 0)
                    dgvCol.Width = itemCol.ColumnWidth;

                if (!itemCol.FormatStr.IsEmpty())
                    dgvCol.DefaultCellStyle.Format = itemCol.FormatStr;
                dgvCol.Name = itemCol.ColumnName;
                dgvCol.ReadOnly = itemCol.IsReadOnly;
                dgvCol.Frozen = itemCol.IsFrozen;

                dgvCol.HeaderText = itemCol.ColumnTitle;
                dgvCol.DataPropertyName = itemCol.ColumnName;
                dgv.Columns.Add(dgvCol);
            }
        }
        /// <summary>
        /// 初始化gridview,可以同时初始化多个
        /// </summary>
        public static void InitGridView(this IFormContainer form, params DataGridView[] dgvs)
        {
            if (DBConfig == null)
            {
                LogHelper.LogObj.Error("初始化GridView列失败，DBConfig 未配置");
                return;
            }
            var frmName = form.ContainerName;

            var cols = DBConfig.GetColumnInfo(new Com.GridView.ArgGetList {FormName = frmName }).data;
            if (cols == null || cols.Count == 0) return;
            if (dgvs == null || dgvs.Length == 0)
            {
                var dgvNames = cols.DistinctBy(s => s.DgvName).Select(s => s.DgvName).ToArray();
                dgvs = new DataGridView[dgvNames.Length];
                for (int i = 0; i < dgvNames.Length; i++)
                {
                    var dgvCtls = form.Controls.Find(dgvNames[i], true);
                    if (dgvCtls != null && dgvCtls.Length > 0)
                    {
                        if (dgvCtls[0] is DataGridView dgv)
                        {
                            dgvs[i] = dgv;
                        }
                    }
                }
            }

            foreach (DataGridView dgv in dgvs)
            {
                if (dgv == null) continue;
                dgv.Columns.Clear();
                dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

                var dgvCols = cols.FindAll(s => s.DgvName == dgv.Name);

                InitGridColumn(dgv, dgvCols);
            }
        }
    }
}